<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * -----------------------------------------------------
 *
 * manage Link
 *
 * @package     manage
 * @subpackage  
 * @category    Link - 友情链接
 * @author      DongChao
 * @since       2015-07-03 version 1.0
 * -----------------------------------------------------    
 */
class Link extends CI_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->model('link_model','link');    // 加载链接模型
		check_login(); // 检查是否登录
	}

	/**
	 * 获取全部数据
	 */
	public function index(){
		$res=$this->link->getAll();
		$this->load->view('admin/link/index',compact("res"));	
	}

	/**
	 * 新增链接
	 */
	public function insert(){
		if(!empty($_POST['name'])){
			//接收表单数据
			$arr 			  =$this->input->post();
			$data['name']	  =$arr['name'];
			$data['href']	  =$arr['content'];
			$data['status']	  =$arr['menu_state'];
			$data['sort']	  =$arr['sort'];

			//将表单数据插入
			if($this->link->insert($data)){
				$this->sys_message->skip('新增链接成功',site_url('manage/link'));
				return false;
			}else{
				$this->sys_message->show_error('新增链接失败');
				return false;
			}
		}

		$this->load->view('admin/link/insert');	
	}

	/**
	 * 更新链接
	 */
	public function edit(){

		//根据用户提交表单修改数据
		if(!empty($_POST['name'])){
			//接收表单数据
			$arr 			   =$this->input->post();
			$data['name']	   =$arr['name'];
			$data['href']      =$arr['content'];
			$data['status']	   =$arr['status'];
			$data['sort']	   =$arr['sort'];
			
			if($this->link->edit($data,$_GET['id'])){
				$this->sys_message->skip('链接更新成功',site_url('manage/link'));
				return false;
			}else{
				$this->sys_message->show_error('链接更新失败');
				return false;
			}
		}

		$id =$this->input->get('id');		//接收链接ID值
		$res=$this->link->getOne($id);		//根据链接查询对应数据
		$this->load->view('admin/link/edit',compact('res'));//显示数据
	}

	/**
	 * 删除指定数据
	 */
	public function delete(){
		if(!empty($_GET['id'])){
			if($this->link->delete($_GET['id'])){
				die('ok');
			}else{
				die('no');
			}
		}
	}
}